/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.warehousekpi.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.warehousekpi.entity.WarehouseKpiReport;
import com.thinkgem.jeesite.modules.warehousekpi.service.WarehouseKpiReportService;

/**
 * 工时计算Controller
 * @author hui.li
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/warehousekpi/warehouseKpiReport")
public class WarehouseKpiReportController extends BaseController {

	@Autowired
	private WarehouseKpiReportService warehouseKpiReportService;
	
	@ModelAttribute
	public WarehouseKpiReport get(@RequestParam(required=false) String id) {
		WarehouseKpiReport entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = warehouseKpiReportService.get(id);
		}
		if (entity == null){
			entity = new WarehouseKpiReport();
		}
		return entity;
	}
	
	@RequiresPermissions("warehousekpi:warehouseKpiReport:view")
	@RequestMapping(value = {"list", ""})
	public String list(WarehouseKpiReport warehouseKpiReport, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WarehouseKpiReport> page = warehouseKpiReportService.findPage(new Page<WarehouseKpiReport>(request, response), warehouseKpiReport); 
		model.addAttribute("page", page);
		return "modules/warehousekpi/warehouseKpiReportList";
	}

	@RequiresPermissions("warehousekpi:warehouseKpiReport:view")
	@RequestMapping(value = "form")
	public String form(WarehouseKpiReport warehouseKpiReport, Model model) {
		model.addAttribute("warehouseKpiReport", warehouseKpiReport);
		return "modules/warehousekpi/warehouseKpiReportForm";
	}

	@RequiresPermissions("warehousekpi:warehouseKpiReport:edit")
	@RequestMapping(value = "save")
	public String save(WarehouseKpiReport warehouseKpiReport, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, warehouseKpiReport)){
			return form(warehouseKpiReport, model);
		}
		warehouseKpiReportService.save(warehouseKpiReport);
		addMessage(redirectAttributes, "保存工时计算成功");
		return "redirect:"+Global.getAdminPath()+"/warehousekpi/warehouseKpiReport/?repage";
	}
	
	@RequiresPermissions("warehousekpi:warehouseKpiReport:edit")
	@RequestMapping(value = "delete")
	public String delete(WarehouseKpiReport warehouseKpiReport, RedirectAttributes redirectAttributes) {
		warehouseKpiReportService.delete(warehouseKpiReport);
		addMessage(redirectAttributes, "删除工时计算成功");
		return "redirect:"+Global.getAdminPath()+"/warehousekpi/warehouseKpiReport/?repage";
	}
	
	@RequiresPermissions("warehousekpi:warehouseKpiReportCost:view")
	@RequestMapping(value = {"costList", ""})
	public String costList(WarehouseKpiReport warehouseKpiReport, HttpServletRequest request, HttpServletResponse response, Model model) {
		// TODO Auto-generated method stub
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
//	    // 获取前月的第一天  
//        Calendar  cale = Calendar.getInstance();  
//        cale.add(Calendar.MONTH, 0);  
//        cale.set(Calendar.DAY_OF_MONTH, 1);  
//        //String firstday = format.format(cale.getTime()); 
//        //System.out.println("本月第一天 " + firstday);
//        // 获取当前日期字符串  
//        Date d = new Date();  
//        //String currenyDay= format.format(d);
//        //System.out.println("当前日期字符串1：" + format.format(d));
//        if(null != warehouseKpiReport)
//        {
//        	if(null  == warehouseKpiReport.getBeginDate())
//        	{
//        		warehouseKpiReport.setBeginDate(cale.getTime());
//        	}
//        	else if(null == warehouseKpiReport.getEndDate())
//        	{
//        		warehouseKpiReport.setEndDate(Calendar.getInstance().getTime());
//        	}
//        }
        
		Page<WarehouseKpiReport> page = warehouseKpiReportService.findCostList(new Page<WarehouseKpiReport>(request, response), warehouseKpiReport); 
		model.addAttribute("page", page);
		model.addAttribute("warehouseKpiReport", warehouseKpiReportService.findCostSummaryList(warehouseKpiReport));
		return "modules/warehousekpi/warehouseKpiReportCostList";
	}

}